package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.icss.beans.NoticeVO;
import com.icss.daos.NoticeDao;

/**
 * @author 廖鹏
 * @version 创建时间：2010-3-17 上午11:25:53
 * 类说明
 */
@SuppressWarnings("serial")
public class NoticeServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		
		out.flush();
		out.close();
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String action=request.getParameter("action");
		if("add".equals(action)){
			addNotice(request,response);
		}
		if("page".equals(action)){
			pageNotice(request,response);
		}
		if("totalRecord".equals(action)){
			totalRecordNotice(request,response);
		}
		if("del".equals(action)){
			delNotice(request,response);
		}
		if("see".equals(action)){
			seeNoticeAction(request,response);
		}
		out.flush();
		out.close();
	}
	
	private void seeNoticeAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int noticeID = Integer.parseInt(request.getParameter("noticeID"));
		NoticeVO noticeVO = null;
		
		NoticeDao noticeDao = new NoticeDao();
		noticeVO = noticeDao.getNotice(noticeID);
		request.setAttribute("noticeVO", noticeVO);
		request.getRequestDispatcher("/page/notice/seeNotice.jsp").forward(request, response);
		response.getWriter().close();

}
	
	
	/**
	 * @author 廖鹏
	 * 功能：输出记录总数
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void totalRecordNotice(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		String startTime = request.getParameter("noticeTime");
		String selectType = request.getParameter("selectType");
		NoticeDao noticeDao = new NoticeDao();
		int i=noticeDao.selectNoticeTotal(startTime, selectType);
		System.out.println("总记录数："+i);
		response.getWriter().println(i);		
	}
	
	/**
	 * 删除通知
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void delNotice(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out=response.getWriter();
		String noticeIDs = request.getParameter("noticeIDs");
		System.out.println(noticeIDs);
		String[] noticeIDS=noticeIDs.split(":");
		int i=0;	
		int len = noticeIDS.length;
		int[] ids=new int[len];
		System.out.println(len);
		while(i<noticeIDS.length){
			ids[i]=Integer.parseInt(noticeIDS[i]);
			System.out.println(noticeIDS[i]);
			i++;
		}
		NoticeDao noticeDao = new NoticeDao();
		
		boolean b=noticeDao.delNotice(ids);
		if(b){
			out.println(1);
		}else{
			out.println(0);
		}
	}
	
	/**
	 * @author 廖鹏
	 * 功能：发布公告
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void addNotice(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException,IOException {
		String noticeTitle=request.getParameter("noticeTitle");
		String noticeContent =request.getParameter("noticeContent");
		NoticeVO noticeVO=new NoticeVO();
		noticeVO.setNoticeTitle(noticeTitle);
		noticeVO.setNoticeContent(noticeContent);
		NoticeDao noticeDao = new NoticeDao();
		boolean b=noticeDao.addNotice(noticeVO);
		if(b){
			PrintWriter out=response.getWriter();
			out.write("<script>eval(alert('发布公告成功'))</script>");
			response.sendRedirect("page/notice/addNotice.jsp");			
			return;
		}
		
	}
	
	/**
	 * @author 廖鹏
	 * 功能：分页显示公告
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	@SuppressWarnings("unchecked")
	private void pageNotice(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		
		PrintWriter outPrintWriter=response.getWriter();
		String noticeTime =request.getParameter("noticeTime");
		String selectType =request.getParameter("selectType");
		int currentPage=Integer.parseInt(request.getParameter("currentPage"));
		int pageSize=Integer.parseInt(request.getParameter("pageSize"));
		System.out.println(currentPage);
		NoticeDao noticeDao=new NoticeDao();
		List<NoticeVO> notiList=noticeDao.getPageNotices(noticeTime, selectType,currentPage,pageSize);
		if(0==notiList.size()){
			outPrintWriter.println(0);
			outPrintWriter.flush();
			outPrintWriter.close();
			return;
		}
		JSONArray jsonNotice=new JSONArray();
		for(int i=0;i<notiList.size();i++){
			Map map=new HashMap();
			map.put("noticeID", notiList.get(i).getNoticeID());
			map.put("noticeTitle", notiList.get(i).getNoticeTitle());
			map.put("noticeTime", notiList.get(i).getNoticeTime());
			map.put("noticeContent", notiList.get(i).getNoticeContent());
			jsonNotice.add(map);
		}
		System.out.println(jsonNotice);
		outPrintWriter.println(jsonNotice);
		outPrintWriter.flush();
		outPrintWriter.close();		
	}

}
